﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;

namespace sWarranty.WEB
{
    public partial class UserMGR : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "UserMGR.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");


            if (!IsPostBack)
            {
                Entities db = new Entities();
                var user = (from t in db.TB_USER
                            orderby t.ACTIVE ascending
                            select t).Take(1000);
                litData.Text = "";
                foreach (var sh in user)
                {
                    litData.Text += "<tr>";
                    litData.Text += "	<td title=''>";
                    litData.Text += sh.EMPNO;
                    litData.Text += "	</td>";
                    litData.Text += "	<td title=''> <a onclick=\"openPopUp('userprofile.aspx?m=1&u=" + sh.EMPNO + "','open')\" href='#' title='Modify Information'>";
                    litData.Text += sh.ACCOUNT;
                    litData.Text += "	</a></td>";
                    litData.Text += "	<td title=''>";
                    litData.Text += sh.DISPLAY_NAME;
                    litData.Text += "	</td>";
                    litData.Text += "	<td title=''>";
                    litData.Text += DBAction.GetAuthorityName(sh.ID); ;
                    litData.Text += "	</td>";
                    litData.Text += "	<td title=''>";
                    litData.Text += DBAction.GetRegionByID(sh.REGION_ID); ;
                    litData.Text += "	</td>";
                    if (sh.ACTIVE == 1)
                    {
                        litData.Text += "	<td title='' >";
                        litData.Text += "Y";
                        litData.Text += "	</td>";
                    }
                    else
                    {
                        litData.Text += "	<td title='' style='color: red;'>";
                        litData.Text += "N";
                        litData.Text += "	</td>";
                    }

                    litData.Text += "</tr>";
                }
                litData.Text += "<input type='text' id='currentPage' value='1' style='display:none;'/>";
                litData.Text += "<!-- DataGrid End -->";
                //Pager 
                litPager.Text += "<div id='pager' class='pager' style='padding: 5px;'>";
                litPager.Text += " <img id='viewPre' src='../images/prev.png' />";
                litPager.Text += "    &nbsp;&nbsp;";
                litPager.Text += "  <a href='#' onclick='nextPage();' style='Clear_both'>  <img id='viewNext' src='../images/next.png' alt='Page 2' />  </a>";
                litPager.Text += "</div>";
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }

        protected void btnRegisterNew_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'UserRegister.aspx', null, 'height:400px,width:700px,status=yes,scrollbars=1,dependent=no,menubar=no,resizable=yes,center:yes' );", true);

        }
    }
}